chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    var currentPageUrl = tabs[0].url;
    // console.log()
    queryStat()
    let host = new URL(currentPageUrl).host

    document.getElementById('currentPageUrl').innerText = "当前页面域名：" + host;
    
    document.querySelector("#startBlock").addEventListener("click", function (params) {
        chrome.runtime.sendMessage({ action: 'add', host }, function (response) {
            // 可以在这里处理从后台脚本返回的响应
            console.log(response);
            queryStat()
        });
    })

    document.querySelector("#closeBlock").addEventListener("click", function (params) {
        chrome.runtime.sendMessage({ action: 'remove', host }, function (response) {
            // 可以在这里处理从后台脚本返回的响应
            console.log(response);
            queryStat()
        });
    })
    async function queryStat(params) {
        chrome.storage.local.get("blockHost", function ({ blockHost }) {
            if (blockHost.includes(host)) {
                document.querySelector("#startBlock").disabled = true;
                document.querySelector("#closeBlock").disabled = false
            } else {
                document.querySelector("#startBlock").disabled = false;
                document.querySelector("#closeBlock").disabled = true
            }
        })

    }
});


function getTwoHost(host = "") {
    return host.split(".").slice(-2).join(".")
}